﻿#include "taskitem.h"
#include <QUuid>

TaskItem::TaskItem(const QString& sName,QObject *parent) : QObject(parent)
{
    msName = sName;
    mdStartTime = 0.0;
    mdEndTime = 0.0;
    msType = QString();
	mvctCommands.clear();

    this->setObjectName(QString("Task:%1:%2").arg(sName).arg(QUuid::createUuid().toString().section("-",1,1)));
}

QString TaskItem::getName()
{
    return msName;
}

void TaskItem::setType(const QString &sType)
{
    msType = sType;
}

QString TaskItem::getType()
{
    return msType;
}

void TaskItem::setValue(ScriptItem::PosType type, double value)
{
    switch(type)
    {
    case ScriptItem::Time_Start:
    {
        mdStartTime = value;
    }
    case ScriptItem::Time_End:
    {
        mdEndTime = value;
    }
    case ScriptItem::Pos_EWStart:
    {
        mdEWStart = value;
    }
    case ScriptItem::Pos_EWEnd:
    {
        mdEWEnd = value;
    }
    case ScriptItem::Pos_NSStart:
    {
        mdNSStart = value;
    }
    case ScriptItem::Pos_NSEnd:
    {
        mdNSEnd = value;
    }
    default:
        break;
    }
}

double TaskItem::getValue(ScriptItem::PosType type)
{
    switch(type)
    {
    case ScriptItem::Time_Start:
    {
        return mdStartTime;
    }
    case ScriptItem::Time_End:
    {
        return mdEndTime;
    }
    case ScriptItem::Pos_EWStart:
    {
        return mdEWStart;
    }
    case ScriptItem::Pos_EWEnd:
    {
        return mdEWEnd;
    }
    case ScriptItem::Pos_NSStart:
    {
        return mdNSStart;
    }
    case ScriptItem::Pos_NSEnd:
    {
        return mdNSEnd;
    }
    default:
        break;
    }

    return 0.0;
}

CommandItem * TaskItem::addCommand(const QString &name)
{
    CommandItem* pCommand = new CommandItem(name,this);
    mvctCommands.append(pCommand);
    pCommand->task = this;
    return pCommand;
}

void TaskItem::removeCommand(const QString &objName)
{
    foreach (CommandItem* pCommand, mvctCommands) {
        if(pCommand->objectName() == objName)
        {
            mvctCommands.removeOne(pCommand);
            delete pCommand;
        }
        else
        {
            continue;
        }
    }
}

QVector<CommandItem *> TaskItem::allCommands()
{
    return mvctCommands;
}

CommandItem *TaskItem::getCommand(const QString &objName)
{
	if (mvctCommands.count() < 1)
		return NULL;

    foreach (CommandItem* pCommand, mvctCommands) {
        if(pCommand->objectName() == objName)
        {
            return pCommand;
        }
        else
        {
            continue;
        }
    }

    return NULL;
}
